# encoding: utf-8

module Capevaluation
  module Dao
    class Example < Sequel::Model(CaiQiu::Core::ServiceConn.DB_EXAMPLE[:example])
      set_primary_key = [:id]

      def self.select_hash_all(params)
        self.select_hash(params).all
      end

      def self.select_hash(params)
        params   = CaiQiu::Support::Utils.hash_key_to_symbol(params)
        eval_str = 'self'
        params.keys.each do |k|
          if self.columns.include? k
            eval_str = "#{eval_str}.where(:#{k} => params[:#{k}])"
          end
        end
        eval_str = "#{eval_str}"
        eval(eval_str)
      end

      def save_hash(params)
        params   = CaiQiu::Support::Utils.hash_key_to_symbol(params)
        eval_str = ''
        params.keys.each do |k|
          eval_str = "#{eval_str} self[:#{k}] = params[:#{k}] if self.columns.include? :#{k};"
        end
        eval_str = "#{eval_str} self.save_changes"
        eval(eval_str)
      end

    end
  end
end
